package com.bsj.power.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.config.global.TokenUser;
import com.bsj.power.common.constant.UserConstants;
import com.bsj.power.common.def.entity.log.SystemOpLog;
import com.bsj.power.common.def.entity.log.VideoOpLog;
import com.bsj.power.common.def.vo.video.log.SystemOpLogListVO;
import com.bsj.power.common.def.vo.video.log.SystemOpLogSearchVO;
import com.bsj.power.common.def.vo.video.log.VideoOpLogListVO;
import com.bsj.power.common.mapper.SystemOpLogMapper;
import com.bsj.power.common.mapper.UserRoleMapper;
import com.bsj.power.common.util.StpLoginUserUtil;
import com.bsj.power.common.util.StringUtils;
import com.bsj.power.system.service.ISystemOpLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhaoyang
 * @Date 2024/8/15 19:10
 */
@Slf4j
@Service
public class SystemOpLogServiceImpl implements ISystemOpLogService {

    @Resource
    private SystemOpLogMapper systemOpLogMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    @Deprecated
    public JsonResult<List<SystemOpLogListVO>> systemOpLogList(SystemOpLogSearchVO systemOpLogSearchVO) {
        List<SystemOpLogListVO> systemOpLogListVOList = new ArrayList<>();
        IPage<SystemOpLog> systemOpLogPage = new Page<>(systemOpLogSearchVO.getPageNumber(), systemOpLogSearchVO.getPageSize());
        QueryWrapper<SystemOpLog> queryWrapper = new QueryWrapper<>();

        // 是超级管理员就查全部，不是超级管理员就查自己
        TokenUser tokenUser = StpLoginUserUtil.getTokenUser();
        Long tokenUserUserId = tokenUser.getUserId();
        Long parentId = tokenUser.getParentId();
        Integer roleType = userRoleMapper.selectUserRoleType(tokenUserUserId);
        if (roleType != 1 && parentId != -1) {
            // 不是系统超级管理员,无法操作
            queryWrapper.eq("op_user_id", tokenUserUserId);
        }

        if (systemOpLogSearchVO.getOpType() != null && systemOpLogSearchVO.getOpType() != 0) {
            queryWrapper.eq("op_type", systemOpLogSearchVO.getOpType());
        }
        if (StringUtils.isNotEmpty(systemOpLogSearchVO.getOpUserName())) {
            queryWrapper.eq("op_user_name", systemOpLogSearchVO.getOpUserName());
        }
        if (systemOpLogSearchVO.getStartTime() != null && systemOpLogSearchVO.getEndTime() != null) {
            queryWrapper.between("op_time", systemOpLogSearchVO.getStartTime(), systemOpLogSearchVO.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<SystemOpLog> iPage = systemOpLogMapper.selectPage(systemOpLogPage, queryWrapper);
        List<SystemOpLog> systemOpLogList = iPage.getRecords();
        long total = iPage.getTotal();
        if (CollectionUtil.isNotEmpty(systemOpLogList)) {
            systemOpLogList.forEach(v -> {
                SystemOpLogListVO videoOpLogListVO = new SystemOpLogListVO();
                BeanUtils.copyProperties(v, videoOpLogListVO);
                systemOpLogListVOList.add(videoOpLogListVO);
            });
        }
        return JsonResult.success(systemOpLogListVOList, total);
    }
}
